package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonCommissionAuthorityMapper;
import com.zhijian.medical.entity.po.CommissionAuthority;
import com.zhijian.medical.entity.po.PersonCommissionAuthority;
import com.zhijian.medical.entity.vo.request.PersonCommissionAuthoritySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonCommissionAuthoritySearchVo;
import com.zhijian.medical.entity.vo.response.PersonCommissionAuthorityVo;
import com.zhijian.medical.service.CommissionAuthorityService;
import com.zhijian.medical.service.PersonCommissionAuthorityService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人员-分级授权书业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-28 17:07:47
 */
@Slf4j
@Service
public class PersonCommissionAuthorityServiceImpl implements PersonCommissionAuthorityService {

    @Autowired
    private PersonCommissionAuthorityMapper mapper;

    @Autowired
    private CommissionAuthorityService commissionAuthorityService;

    @Override
    public PersonCommissionAuthority selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonCommissionAuthorityVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public void deleteAllByCommissionAuthorityId(Long commissionAuthorityId) {
        commissionAuthorityService.deleteById(commissionAuthorityId);
        this.deleteByCommissionAuthorityId(commissionAuthorityId);
    }

    @Override
    public void deleteByCommissionAuthorityId(Long commissionAuthorityId) {
        mapper.deleteByCommissionAuthorityId(commissionAuthorityId);
    }

    @Override
    public Long insert(PersonCommissionAuthority personCommissionAuthority) {
        Date now = new Date();
        personCommissionAuthority.setCreateTime(now);
        personCommissionAuthority.setUpdateTime(now);
        return mapper.insert(personCommissionAuthority) > 0 ? personCommissionAuthority.getId() : 0L;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean saveOrUpdate(PersonCommissionAuthoritySaveOrUpdateVo personCommissionAuthoritySaveOrUpdateVo) {
        Long commissionAuthorityId = personCommissionAuthoritySaveOrUpdateVo.getCommissionAuthorityId();
        if (Objects.isNull(commissionAuthorityId)) {
            //如果此id为空，说明是新增的项目名称
            commissionAuthorityId = commissionAuthorityService.insert(CommissionAuthority.builder()
                    .projectName(personCommissionAuthoritySaveOrUpdateVo.getProjectName())
                    .build());
        }
        //查找有无关系记录
        PersonCommissionAuthority personCommissionAuthority = this.selectByPersonIdAndCommissionAuthorityId(personCommissionAuthoritySaveOrUpdateVo.getPersonId(), commissionAuthorityId);
        if (Objects.isNull(personCommissionAuthority)) {
            //没有的话，保存
            this.insert(PersonCommissionAuthority.builder()
                    .personId(personCommissionAuthoritySaveOrUpdateVo.getPersonId())
                    .commissionAuthorityId(commissionAuthorityId)
                    .weatherEmpower(personCommissionAuthoritySaveOrUpdateVo.getWeatherEmpower())
                    .build());
        } else {
            //有的话，修改
            personCommissionAuthority.setWeatherEmpower(personCommissionAuthoritySaveOrUpdateVo.getWeatherEmpower());
            this.update(personCommissionAuthority);
        }
        return true;
    }

    @Override
    public boolean update(PersonCommissionAuthority personCommissionAuthority) {
        Date now = new Date();
        personCommissionAuthority.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personCommissionAuthority) != 0;
    }

    @Override
    public Integer count(PersonCommissionAuthoritySearchVo personCommissionAuthoritySearchVo) {
        return mapper.count(personCommissionAuthoritySearchVo);
    }

    @Override
    public List<PersonCommissionAuthorityVo> selectList(PersonCommissionAuthoritySearchVo personCommissionAuthoritySearchVo) {
        return mapper.selectList(personCommissionAuthoritySearchVo);
    }

    @Override
    public Page<PersonCommissionAuthorityVo> selectPage(PersonCommissionAuthoritySearchVo personCommissionAuthoritySearchVo) {
        //先直接查总数
        int count = count(personCommissionAuthoritySearchVo);
        //分页
        Page<PersonCommissionAuthorityVo> page = new Page<>(true, count, personCommissionAuthoritySearchVo.getPageNum(), personCommissionAuthoritySearchVo.getPageSize());
        personCommissionAuthoritySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonCommissionAuthorityVo> responseVos = this.selectList(personCommissionAuthoritySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public PersonCommissionAuthority selectByPersonIdAndCommissionAuthorityId(Long personId, Long commissionAuthorityId) {
        return mapper.selectByPersonIdAndCommissionAuthorityId(personId, commissionAuthorityId);
    }

}